<?php
//首页传世模块
	namespace Application\Admin\Controller;
	use Application\Admin\Model\Chuanshi as ChuanshiModel;
	use KJ\Upload;
	class Chuanshi extends Admin{
		public  function index(){
			$ChuannshiModel = new ChuanshiModel();
			$this->cat = $ChuannshiModel -> select();
			$this -> render();
		}
		
		public  function add(){
			if(IS_POST){
				$ChuanshiModel = new ChuanshiModel();
				if($_FILES['image']['size'] != 0){
					$upload = new Upload();
					$result = $upload -> upload('image');
					if(!empty($result['error'])){
						echo "<script>alert('上传信息有误');
						window.history.go(-1);</script>";
						exit;
					}else{
						$ChuanshiModel -> attributes -> img_src = $result['realPath'];
					}
				}
				$ChuanshiModel -> attributes -> h3_name = $_POST['h3_name'];
				$ChuanshiModel -> attributes -> h4_name = $_POST['h4_name'];
				$ChuanshiModel -> attributes -> link = $_POST['link'];
				$ChuanshiModel -> attributes -> sort = $_POST['sort'];
				if($ChuanshiModel -> save()){
					echo "<script>alert('插入成功');
					window.location.href='index.php?m=admin&c=chuanshi&a=index';</script>";
					exit;
				}
				
			}
			$this -> render('add');
		}		
		
		public function delete(){
			if(IS_GET){
				$id = $_GET['id'];
				$ChuanshiModel = new ChuanshiModel();
				$chuanshi = $ChuanshiModel ->  findModel($id);
				$src = $ChuanshiModel -> field('img_src') -> where("id = $id") -> select();
				$src = $src[0]['img_src'];
				if($chuanshi -> delete()){
					if(is_file($src)){
						unlink($src);
					}
					echo "<script>alert('删除成功');window.location.href='index.php?m=admin&c=chuanshi&a=index';</script>";
				}else{
					echo "<script>alert('删除失败');window.location.href='index.php?m=admin&c=chuanshi&a=index';</script>";
				}
			}
		}	
		
		public function edit(){
			if(IS_GET){
				
				$id = $_GET['id'];
				
				$ChuanshiModel = new ChuanshiModel();
				
				$this->Chuanshi = $ChuanshiModel -> find($id);
				
				$this->render('edit');
			}
			if(IS_POST){
				$id = $_POST['id'];
				$ChuanshiModel = new ChuanshiModel();
				$Chuanshi = $ChuanshiModel -> findModel($id);
				if($_FILES['image']['size'] != 0){
					$upload = new Upload();
					$upload -> EXTS = array('jpeg','jpg','png');
					$result = $upload -> upload('image');
					if(!empty($result['error'])){
						echo "<script>alert('上传信息有误');window.history.go(-1);</script>";
						exit;
					}else{
						$Chuanshi -> attributes -> img_src = $result['realPath'];
					}
				}
				$ChuanshiModel -> attributes -> h3_name = $_POST['h3_name'];
				$ChuanshiModel -> attributes -> h4_name = $_POST['h4_name'];
				$ChuanshiModel -> attributes -> link = $_POST['link'];
				$ChuanshiModel -> attributes -> sort = $_POST['sort'];
				if($Chuanshi -> save()){
					echo "<script>alert('编辑成功');window.location.href='index.php?m=admin&c=chuanshi&a=index';</script>";
					exit;
				}
			}
			
		}		
		
	}
?>